<?php
if($this->session->userdata('role') != null){
	if($this->session->userdata('role') == 1){
include 'include/header.php';
?>
<script>
function modal_preview(e){
				e.preventDefault();
				$('#ajaxModal').remove();
				var url = $(e.target).attr('href');
				$('<div class="modal" id="ajaxModal"><div class="modal-body"></div></div>').appendTo('body');
				 $('#ajaxModal').modal();
				 $('#ajaxModal').load(url);
				//console.log(url.toString());
}
</script>
<div style="background-color:#fff; margin-top:-15px; margin-left: 150px; margin-right: 150px; box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.1)">
	<div style="margin-left: 25px; margin-right: 25px; padding-top:15px; padding-bottom:15px">
		
		<h3>Edit Kuesioner Penelitian</h3>
		<form  id="formE" method="post" action="<?php echo base_url().'index.php/kuesioner/update_kuesioner/'?>" enctype="multipart/form-data" data-parsley-validate>
			<div class="form-group">
				<label>Nama Kuesioner</label>
				<input class="form-control" name="nama" data-parsley-trigger="change" required value="<?php echo $kuesioner[0]['nama_kuesioner']?>">
				<input class="form-control" name="id_kues" data-parsley-trigger="change" required value="<?php echo $kuesioner[0]['id_kuesioner']?>" style="display:none">
			</div>
			<div class="form-group">
				<label>Deskripsi</label>
				<input class="form-control" name="deskripsi" data-parsley-trigger="change" value="<?php echo $kuesioner[0]['deskripsi']?>" required>
			</div>
			<div class="form-group">				
						<div id="identitas">
						<label>Identitas Responden</label>
						<div><input type="button" class="btn btn-warning" value="tambah pertanyaan identitas" onclick="tambah_id_baru()"></div>
						<?php
						$id_teks = 0;
							foreach($konten_teks as $konten_teks)
							{
								$id_teks++;
								echo '<div id="identitas-'.$id_teks.'" class="row">';
								echo '<div class="col-lg-4">';
								echo '<div class="input-group">';
								echo '<input name="id_pertanyaan[]" id="id_kues_teks-'.$id_teks.'"style="display:none" value="'.$konten_teks['id_pertanyaan'].'">';
								echo '<input class="form-control" name="jns_kues_lama[]" value="1" style="display:none">';
								echo '<input type="text" class="form-control" name="pertanyaan[]" data-parsley-trigger="change" required value="'.$konten_teks['pertanyaan'].'">';
								echo '<span class="input-group-btn"><button type="button" class="btn btn-default" onclick="delete_teks_lama('.$id_teks.')">X</button></span>';
								echo '</div>';
								echo '</div>';
								echo '</div>';
							
							}
						?>
							<div id="kolom_tambah_identitas"></div>
						</div>
						<div id="pilgan">
						<label>Pertanyaan pilihan</label>
						<div><input type="button" class="btn btn-warning" value="tambah pertanyaan pilihan" onclick="modal_pg()"></div>
						<?php
						$a = 1;
							foreach($konten_pg as $konten_pg)
							{
								$id_teks++;
								echo '<div id="kolom_pg_lama-'.$id_teks.'" class="row">';
								echo '<div><button type="button" class="btn btn-default" onclick="delete_pg_lama('.$id_teks.')"  title="Hapus pertanyaan pilihan ganda!" style="margin-left: 960px; padding-bottom: 5px;">X</button></div>';
								echo '<div class="col-lg-4">';
								echo '<div class="input-group">';
								echo '<input name="id_pertanyaan[]" id="id_pg_lama-'.$id_teks.'"style="display:none" value="'.$konten_pg['id_pertanyaan'].'">';
								echo '<input class="form-control" name="jns_kues_lama[]" value="2" style="display:none">';
								echo '<input type="text" class="form-control" name="pertanyaan[]" data-parsley-trigger="change" required value="'.$konten_pg['pertanyaan'].'">';
								echo '</div>';
								
								echo '</div>';
								
								$exp_pg = explode(',', $konten_pg['pilihan']);
								$jumlah_pilihan = count($exp_pg)-1;
								$indeks_pg = 0;
								$c = floor($jumlah_pilihan/5);
								$d = $jumlah_pilihan%5;
								echo '<input name="id_pilihan_lama-'.$konten_pg['id_pertanyaan'].'" id="id_pilihan_lama-'.$id_teks.'" style="display:none" value="'.$konten_pg['id_pilihan_ganda'].'">';
								for($j = 0; $j < $c; $j++){
								echo '<div class="row" style="margin-left: 370px; width: 1176">';
									for($i=0; $i < 5; $i++) {
										echo '<div class="col-lg-2">';
										echo '<input class="form-control" style="border-style:ridge;" name="pg_lama_'.$konten_pg['id_pertanyaan'].'[]" style="margin-lelft:-15px; width:94px" data-parsley-trigger="change" value="'.$exp_pg[$indeks_pg++].'" required>';
										echo '</div>';
									}
								echo '</div>';
								}
								
								echo '<div class="row" style="margin-left: 370px; width: 1176">';
								for($k = 0; $k < $d; $k++){
										echo '<div class="col-lg-2">';
										echo '<input class="form-control" style="border-style:ridge;" name="pg_lama_'.$konten_pg['id_pertanyaan'].'[]" style="margin-lelft:-15px; width:94px" data-parsley-trigger="change" value="'.$exp_pg[$indeks_pg++].'" required>';
										echo '</div>';
								}
								
								echo '</div>';
								//echo '</div>';
								echo '</div>';
							}
						?>
							<div id="kolom_tambah_pilgan"></div>
						</div>
						<div id="kolom_skala">
						<label>Skala</label>
						<div><input type="button" class="btn btn-warning" id="btn_tambah_skala" value="tambah skala" onclick="modal_skala()" disabled></div>
						<?php
						if(empty($skala[0]))
						{
							
						} else
							{
									foreach($skala as $skala)
								{
									$id_teks++;
									echo '<div id="skala" class="row">';
									echo '<div class="col-lg-4">';
									echo '<div class="input-group">';
									echo '<button type="button" class="btn btn-default" onclick="delete_skala_lama()">X</button>';
									echo '<input name="id_skala" id="id_skala-'.$id_teks.'"style="display:none" value="'.$skala['id_skala'].'">';
									$exp_skala = explode(',', $skala['skala']);
										//var_dump($skala);
										for($i = 0; $i < count($exp_skala)-1; $i++)
										{
											echo '<input type="text" class="form-control" name="skala[]" data-parsley-trigger="change" required value="'.$exp_skala[$i].'">';
										}
									
									echo '</div>';
									echo '</div>';
									echo '</div>';
								
								}
							}
						?>
							<div id="kolom_tambah_skala"></div>
						</div>
						<div id="kolom_pertanyaan">
						<label>Pertanyaan Skala</label>
						<div><input type="button" class="btn btn-warning" value="tambah pertanyaan" onclick="nambah_pert_skala()"></div>
						<?php
							foreach($konten_skala as $konten_skala)
							{
							$id_teks++;
								echo '<div id="pert_skala-'.$id_teks.'" class="row">';
								echo '<div class="col-lg-4">';
								echo '<div class="input-group">';
								echo '<input name="id_pertanyaan[]" id="id_kues_skala-'.$id_teks.'"style="display:none" value="'.$konten_skala['id_pertanyaan'].'">';
								echo '<input class="form-control" name="jns_kues_lama[]" value="3" style="display:none">';
								echo '<input type="text" class="form-control" name="pertanyaan[]" data-parsley-trigger="change" required value="'.$konten_skala['pertanyaan'].'">';
								echo '<span class="input-group-btn"><button type="button" class="btn btn-default" onclick="delete_pert_skala_lama('.$id_teks.')">X</button></span>';
								echo '</div>';
								echo '</div>';
								echo '</div>';
							
							}
						?>
							<div id="kolom_tambah_pert_skala"></div>
						</div>
				
			</div>
		<!--div class="form-group">
			<label>Upload Proposal</label>
				<input type="file" name="file" id="file" readonly="true" multiple="multiple" data-parsley-trigger="change">
			</div-->
		<!--div class="form-group">
			<label>Keterangan</label>
			<input type="checkbox" id="pp" name="pp" value="1">Publish Penelitian</input>
			<input type="checkbox" id="pm" name="pm" value="1">Publish Metodologi</input>
			<input type="checkbox" id="pd" name="pd" value="1">Publish Data</input>
		</div-->
		<div class="form-group">
		  <button type="submit" class="btn btn-primary" onclick="post(event)">Save</button>
		  <input type="button" class="btn btn-danger" value="Cancel" onclick="window.history.back()">
		  <!--input type="button" class="btn btn-warning" value="Preview" onclick="modal_preview(event)" href="<?php //echo site_url('kuesioner/tambah_kuesioner/preview');?>"-->
		</div>
	<div id="modal_skala" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
		<div class="modal-dialog modal-sm">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Pertanyaan skala</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<!--input id="jumlah_skala" class="form-control" placeholder="Jumlah skala"-->
						<select id="jumlah_skala" class="btn btn-default" style="width:100%" >
							<option value=0>-- Pilih Skala--</option>
							<option value=3>3</option>
							<option value=5>5</option>
							<option value=7>7</option>
						</select>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
					<button id="btn_tambah" type="button" class="btn btn-primary" onclick="tambah_skala()" disabled>Tambah</button>
				</div>
			</div>
		</div>
	</div>
		</form>
		
	</div>
</div>
<div id="modal_pg" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
		<div class="modal-dialog modal-sm">
			<div class="modal-content">
				<div class="modal-header">
					<h4 class="modal-title" id="myModalLabel">Pilihan Ganda</h4>
				</div>
				<div class="modal-body">
					<div class="form-group">
						<input id="jumlah_pg" type="number" class="form-control" placeholder="Masukkan jumlah pilihan">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
					<button id="btn_tambah_pg" type="button" class="btn btn-primary" onclick="tambah_kues_pg()" disabled>Tambah</button>
				</div>
			</div>
		</div>
	</div>
<?php
include 'include/footer.php';
} else{ 
		echo 'Anda belum login, silahkan login terlebih dahulu dengan klik link berikut';
		echo '<br /><a href="'.base_url('index.php/guest').'">Kembali</a>';
	}
} else
	{
		echo 'Anda belum login, silahkan login terlebih dahulu dengan klik link berikut';
		echo '<br /><a href="'.base_url('index.php/guest').'">Kembali</a>';
	}
	
?>
<script>

var ident_number = 0;
var kues_teks_number = 0;
var kues_skala_number = 0;
var kues_pg_number =0;

$(document).ready(function() {
	<?php //echo ($kuesioner[0]['publish'] == 1)? "$('#pp').prop('checked', true);" : '' ?>
	<?php //echo ($kuesioner[0]['publish_metodologi'] == 1)? "$('#pm').prop('checked', true);" : '' ?>
	<?php //echo ($kuesioner[0]['publish_data'] == 1)? "$('#pd').prop('checked', true);" : '' ?>

	$('#jumlah_skala').change(function(){
			if($(this).val() != 0){
				$('#btn_tambah').removeAttr('disabled');
			} else{
				$('#btn_tambah').attr('disabled', 'disabled');
				}
		});
		
		$('#jumlah_pg').keyup(function(){
			if($(this).val() == ''){
				$('#btn_tambah_pg').attr('disabled', 'disabled');
			} else {
				$('#btn_tambah_pg').removeAttr('disabled');
			}
		});
		
		$("#formE").on('submit', function(e){
			var a = confirm('Update kuesioner akan menghapus data responden lama. Maka disarankan untuk membackup data responden terlebih dahulu jika diperlukan. Lanjutkan?');
			if(!a){
			e.preventDefault(); 
			return false;
			} else{ 
		
				if(!$('#identitas').children('.row').length > 0 && !$('#pilgan').children('.row').length > 0 && !$('#kolom_pertanyaan').children('.row').length > 0 && !$('#kolom_tambah_identitas').children('.row').length > 0 && !$('#kolom_tambah_pilgan').children('.row').length > 0 && !$('#kolom_tambah_pert_skala').children('.row').length > 0 && !$('#kolom_tambah_skala').children('.row').length > 0 && !$('#kolom_skala').children('.row').length > 0)
				{
					alert('Pertanyaan belum ditambah!');
					e.preventDefault();
					return false;
				}
				
				if(!$('#kolom_pertanyaan').children('.row').length > 0 && $('#kolom_skala').children('.row').length > 0)
				{
					if(!$('#kolom_tambah_pert_skala').children('.row').length > 0 && $('#kolom_skala').children('.row').length > 0)
					{
						alert('Pertanyaan skala belum ditentukan!');
						e.preventDefault();
						return false;
					}
				} 
					
				if(!$('#kolom_pertanyaan').children('.row').length > 0 && $('#kolom_tambah_skala').children('.row').length > 0)
				{
					if(!$('#kolom_tambah_pert_skala').children('.row').length > 0 && $('#kolom_tambah_skala').children('.row').length > 0)
					{
						alert('Pertanyaan skala belum ditentukan!');
						e.preventDefault();
						return false;		
					}
				}
					

				if($('#kolom_pertanyaan').children('.row').length > 0 && !$('#kolom_skala').children('.row').length > 0)
				{
					if($('#kolom_pertanyaan').children('.row').length > 0 && !$('#kolom_tambah_skala').children('.row').length > 0){
					alert('Skala belum ditentukan!');
					e.preventDefault();
					return false;
					}
				}
				
				if($('#kolom_tambah_pert_skala').children('.row').length > 0 && !$('#kolom_skala').children('.row').length > 0)
				{
					if($('#kolom_tambah_pert_skala').children('.row').length > 0 && !$('#kolom_tambah_skala').children('.row').length > 0){
					alert('Skala belum ditentukan!');
					e.preventDefault();
					return false;
					}
				}
			}
		});
		
	
	if(!$('#kolom_skala').children('.row').length > 0)
	{
		$('#btn_tambah_skala').removeAttr('disabled');
	}
	
})
	function tambah_id_baru(){
			ident_number++;
			var identitas = '<div id="row_ident-'+ident_number+'" class="row">';
			identitas += '<div class="col-lg-4">';
			identitas += '<div class="input-group">';
			identitas += '<input name="jns_kues[]" value="1" style="display:none">';
			identitas += '<input type="text" class="form-control" id="add_ident-'+ident_number+'" name="kues[]" data-parsley-trigger="change" required>';
			identitas += '<span class="input-group-btn"><button type="button" class="btn btn-default" onclick="delete_ident('+ident_number+')">X</button></span>';
			identitas += '</div>';
			identitas += '</div>';
			identitas += '</div>';
			$('#identitas').append(identitas);
			}
	
	
	function delete_ident(id) {
		$('#row_ident-'+id).remove();
	}
	
	function delete_pert_skala(id) {
		$('#row_pert_skala-'+id).remove();
	}
	
	function modal_pg()
	{
		$('#modal_pg').modal('show');
	}
	
	function tambah_kues_pg()
	{	var jml = $('#jumlah_pg').val();
		var b = parseInt(jml);
		var c;
		if(b < 5){
			c = 0;
		} else{
			c = (b/5).toFixed();
			}
		var d = b%5;
		var a = 1;
		
		kues_pg_number++;
		var html = '<div class="row" id="kues_pg-'+kues_pg_number+'">';
			html +='<div><button type="button" class="btn btn-default" onclick="delete_pg('+kues_pg_number+')"  title="Hapus pertanyaan pilihan ganda!" style="margin-left: 960px; padding-bottom: 5px;">X</button></div>';
			html += '<div class="col-lg-4">';
			html += '<div class="input-group-btn">';
			html += '<input class="form-control" name="jns_kues[]" value="2" style="display:none">';
			html += '<input class="form-control" placeholder="masukkan pertanyaan" name="kues[]" data-parsley-trigger="change" required>';
			html += '</div>';
			html += '</div>';
			
			for(var j = 0; j < c; j++){
			html += '<div class="row" style="margin-left: 370px; width: 1176">';
				for(var i=0; i < 5; i++) {
					html += '<div class="col-lg-2">';
					html += '<input class="form-control" style="border-style:ridge;" name="pg[]" placeholder="pilihan '+a+'" style="margin-lelft:-15px; width:94px" data-parsley-trigger="change" required>';
					html += '</div>';
					a++;
				}
			html += '</div>';
			}
			
			html += '<div class="row" style="margin-left: 370px; width: 1176">';
			for(var k = 0; k < d; k++){
					html += '<div class="col-lg-2">';
					html += '<input class="form-control" style="border-style:ridge;" name="pg[]" placeholder="pilihan '+a+'" style="margin-lelft:-15px; width:94px" data-parsley-trigger="change" required>';
					html += '</div>';
				a++;
			}
			html += '</div>';
			
			html += '</div>';
			
		
		$('#kolom_tambah_pilgan').append(html);
		$('#modal_pg').modal('hide');
			
	}
	
	function nambah_pert_skala()
	{	
		var html = '<div id="row_pert_skala-'+ident_number+'" class="row">';
			html += '<div class="col-lg-4">';
			html += '<div class="input-group">';
			html += '<input name="jns_kues[]" value="3" style="display:none">';
			html += '<input type="text" class="form-control" id="add_pert_skala-'+ident_number+'" name="kues[]" data-parsley-trigger="change" required>';
			html += '<span class="input-group-btn"><button type="button" class="btn btn-default" onclick="delete_pert_skala('+ident_number+')">X</button></span>';
			html += '</div>';
			html += '</div>';
			html += '</div>';
		$('#kolom_tambah_pert_skala').append(html);
	}
	
	function delete_teks_lama(id)
	{
		$.post('<?php echo base_url(); ?>index.php/kuesioner/delete_pertanyaan/', {
            id_pertanyaan: $('#id_kues_teks-'+id).val()
        });
		$('#identitas-'+id).remove();
	}
	
	function delete_skala_lama()
	{
		$('#skala').remove();
		$('#btn_tambah_skala').removeAttr('disabled');
	}
	
	function delete_pert_skala_lama(id)
	{
		$.post('<?php echo base_url(); ?>index.php/kuesioner/delete_pertanyaan/', {
            id_pertanyaan: $('#id_kues_skala-'+id).val()
        });
		$('#pert_skala-'+id).remove();
	}
	
	function delete_pg_lama(id)
	{
		$.post('<?php echo base_url(); ?>index.php/kuesioner/delete_pertanyaan/', {
            id_pertanyaan: $('#id_pg_lama-'+id).val(),
            id_pg: $('#id_pilihan_lama-'+id).val()
        });
		$('#kolom_pg_lama-'+id).remove();
	}
	
	function delete_pg(id)
	{
		$('#kues_pg-'+id).remove();
	}
	
	function modal_skala()
	{
		$('#modal_skala').modal('show');
	}
	
	function tambah_skala()
	{
		var a = $('#jumlah_skala').val();
		var html = '<div id="skala" class="row">';
		html += '<div class="col-lg-4">';
		html += '<div class="input-group">';
		html += '<button type="button" class="btn btn-default" onclick="delete_skala_lama()" style="margin-left: 947px; position: absolute;">X</button>';
		for($i = 0; $i < a; $i++)
		{
		html += '<input type="text" class="form-control" name="skala_baru[]" data-parsley-trigger="change" required>';
		}						
		html += '</div>';
		html += '</div>';
		html += '</div>';
		$('#kolom_tambah_skala').append(html);
		$('#btn_tambah_skala').attr('disabled', 'disabled');
		$('#modal_skala').modal('hide');
	}
	
	function post_cancel(e)
	{
		
	}

</script>
